import 'package:intl/intl.dart';

/// 历史天数枚举
enum CRPHistoryDay {
  TODAY,
  YESTERDAY,
  DAYS_AGO_2,
  DAYS_AGO_3,
  DAYS_AGO_4,
  DAYS_AGO_5,
  DAYS_AGO_6,
  DAYS_AGO_7,
  DAYS_AGO_8,
  DAYS_AGO_9,
  DAYS_AGO_10,
  DAYS_AGO_11,
  DAYS_AGO_12,
  DAYS_AGO_13,
  DAYS_AGO_14,
}

/// 历史天数辅助类
class HistoryDayHelper {
  /// 将历史天数枚举转换为字符串
  static String historyDayToString(CRPHistoryDay historyDay) {
    switch (historyDay) {
      case CRPHistoryDay.TODAY:
        return 'TODAY';
      case CRPHistoryDay.YESTERDAY:
        return 'YESTERDAY';
      case CRPHistoryDay.DAYS_AGO_2:
        return 'DAYS_AGO_2';
      case CRPHistoryDay.DAYS_AGO_3:
        return 'DAYS_AGO_3';
      case CRPHistoryDay.DAYS_AGO_4:
        return 'DAYS_AGO_4';
      case CRPHistoryDay.DAYS_AGO_5:
        return 'DAYS_AGO_5';
      case CRPHistoryDay.DAYS_AGO_6:
        return 'DAYS_AGO_6';
      case CRPHistoryDay.DAYS_AGO_7:
        return 'DAYS_AGO_7';
      case CRPHistoryDay.DAYS_AGO_8:
        return 'DAYS_AGO_8';
      case CRPHistoryDay.DAYS_AGO_9:
        return 'DAYS_AGO_9';
      case CRPHistoryDay.DAYS_AGO_10:
        return 'DAYS_AGO_10';
      case CRPHistoryDay.DAYS_AGO_11:
        return 'DAYS_AGO_11';
      case CRPHistoryDay.DAYS_AGO_12:
        return 'DAYS_AGO_12';
      case CRPHistoryDay.DAYS_AGO_13:
        return 'DAYS_AGO_13';
      case CRPHistoryDay.DAYS_AGO_14:
        return 'DAYS_AGO_14';
    }
  }

  /// 根据历史天数枚举获取对应的日期字符串 (yyyy-MM-dd格式)
  static String getDailyValue(CRPHistoryDay historyDay) {
    final now = DateTime.now();
    DateTime targetDate;

    switch (historyDay) {
      case CRPHistoryDay.TODAY:
        targetDate = now;
        break;
      case CRPHistoryDay.YESTERDAY:
        targetDate = now.subtract(const Duration(days: 1));
        break;
      case CRPHistoryDay.DAYS_AGO_2:
        targetDate = now.subtract(const Duration(days: 2));
        break;
      case CRPHistoryDay.DAYS_AGO_3:
        targetDate = now.subtract(const Duration(days: 3));
        break;
      case CRPHistoryDay.DAYS_AGO_4:
        targetDate = now.subtract(const Duration(days: 4));
        break;
      case CRPHistoryDay.DAYS_AGO_5:
        targetDate = now.subtract(const Duration(days: 5));
        break;
      case CRPHistoryDay.DAYS_AGO_6:
        targetDate = now.subtract(const Duration(days: 6));
        break;
      case CRPHistoryDay.DAYS_AGO_7:
        targetDate = now.subtract(const Duration(days: 7));
        break;
      case CRPHistoryDay.DAYS_AGO_8:
        targetDate = now.subtract(const Duration(days: 8));
        break;
      case CRPHistoryDay.DAYS_AGO_9:
        targetDate = now.subtract(const Duration(days: 9));
        break;
      case CRPHistoryDay.DAYS_AGO_10:
        targetDate = now.subtract(const Duration(days: 10));
        break;
      case CRPHistoryDay.DAYS_AGO_11:
        targetDate = now.subtract(const Duration(days: 11));
        break;
      case CRPHistoryDay.DAYS_AGO_12:
        targetDate = now.subtract(const Duration(days: 12));
        break;
      case CRPHistoryDay.DAYS_AGO_13:
        targetDate = now.subtract(const Duration(days: 13));
        break;
      case CRPHistoryDay.DAYS_AGO_14:
        targetDate = now.subtract(const Duration(days: 14));
        break;
    }

    return DateFormat('yyyy-MM-dd').format(targetDate);
  }

  /// 构建单个历史数据项
  /// [list] 数据列表
  /// [historyDay] 历史天数枚举
  static Map<String, dynamic> buildHistoryDataItem({
    required List list,
    required CRPHistoryDay historyDay,
  }) {
    return {
      'list': list,
      'dailyValue': getDailyValue(historyDay),
      'historyDay': historyDayToString(historyDay),
    };
  }

  /// 构建多个历史数据项
  /// [dataMap] 键为历史天数枚举，值为对应的数据列表
  static List<Map<String, dynamic>> buildHistoryDataList(
    Map<CRPHistoryDay, List> dataMap,
  ) {
    return dataMap.entries.map((entry) {
      return buildHistoryDataItem(
        list: entry.value,
        historyDay: entry.key,
      );
    }).toList();
  }
}
